package cn.iocoder.yudao.module.enterprise.controller.admin.boundary;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.enterprise.controller.admin.boundary.vo.BoundaryCreateReqVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.boundary.vo.BoundaryRespVO;
import cn.iocoder.yudao.module.enterprise.controller.admin.boundary.vo.BoundaryUpdateReqVO;
import cn.iocoder.yudao.module.enterprise.convert.boundary.BoundaryConvert;
import cn.iocoder.yudao.module.enterprise.dal.dataobject.boundary.BoundaryDO;
import cn.iocoder.yudao.module.enterprise.service.boundary.BoundaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 组织边界")
@RestController
@RequestMapping("/enterprise/boundary")
@Validated
public class BoundaryController {

    @Resource
    private BoundaryService boundaryService;

    @PostMapping("/create")
    @Operation(summary = "创建组织边界")
    @PreAuthorize("@ss.hasPermission('enterprise:boundary:create')")
    public CommonResult<Long> createBoundary(@Valid @RequestBody BoundaryCreateReqVO createReqVO) {
        return success(boundaryService.createBoundary(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新组织边界")
    @PreAuthorize("@ss.hasPermission('enterprise:boundary:update')")
    public CommonResult<Boolean> updateBoundary(@Valid @RequestBody BoundaryUpdateReqVO updateReqVO) {
        boundaryService.updateBoundary(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得组织边界")
    @Parameter(name = "planId", description = "计划编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('enterprise:boundary:query')")
    public CommonResult<BoundaryRespVO> getBoundary(@RequestParam("planId") Long planId) {
        BoundaryDO boundary = boundaryService.getBoundaryByPlanId(planId);
        return success(BoundaryConvert.INSTANCE.convert(boundary));
    }

}
